"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.opggIndex = void 0;
const cheerio_1 = __importDefault(require("cheerio"));
const log4js_1 = require("log4js");
const utils_1 = require("../utils");
const dayjs_1 = __importDefault(require("dayjs"));
const INDEXURL = 'https://www.op.gg/champion/statistics';
const logger = (0, log4js_1.getLogger)('opgg');
function opggIndex(hero, col, qiniuClient, hero_alis) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = yield (0, utils_1.getAxios)(INDEXURL);
        console.log("start");
        if (data === '404') {
            logger.error(`opgg index axios is 404`);
            return;
        }
        const $ = cheerio_1.default.load(data);
        const arrs = ['top', 'jug', 'mid', 'adc', 'sup'];
        const first = $('.detail-ranking__wrapper--content').eq(0);
        const lists = $('.detail-ranking__content--champ-list', first);
        for (const list of lists) {
            const lss = $('ul', list).children();
            for (const item of lss) {
                const name = $('.champion-ratio__name', item).text().replace(/\s/g, '');
                const win = $('.champion-ratio__percent div', item).eq(0).text();
                const pick = $('.champion-ratio__percent div', item).eq(1).text();
                const tier = $('.champion-ratio__tier img', item).attr('src');
                let img = hero_alis.get(name) || "";
                if (img.length === 0) {
                    img = `https://game.gtimg.cn/images/lol/act/img/champion/${name}.png`;
                }
                if (!tier)
                    continue;
                const date = new Date();
                const res = {
                    _id: `${arrs[0]}_${name}`,
                    name_en: name.replace(/\s/g, ''),
                    name_zh: hero.get(name) || "",
                    position: arrs[0],
                    winner: win,
                    pick: pick,
                    img: img,
                    tier: Number(tier.replace('//opgg-static.akamaized.net/images/site/champion/icon-champtier-', '').split('.')[0]),
                    updateTime: date,
                    updateTimeStr: (0, dayjs_1.default)(date).add(8, 'h').format("YYYY-MM-DD HH:mm:ss")
                };
                console.log(JSON.stringify(res));
                try {
                    yield col.updateOne({ _id: res._id }, { $set: res }, { upsert: true });
                }
                catch (error) {
                    logger.error(`updateone is error ${error.message}`);
                }
            }
            arrs.shift();
        }
    });
}
exports.opggIndex = opggIndex;
